{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对象自省( introspection)\n",
    "    自省(introspection)，在计算机编程领域里，是指在运行时来判断一个对象的类型的能力。它是Python的强项之一。Python中所有一切都是一个对象，而且我们可以仔细勘察那些对象。Python还包含了许多内置函数和模块来帮助我们。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数缓存 (lru_cache)\n",
    "    函数缓存允许我们将一个函数对于给定参数的返回值缓存起来。\n",
    "    当一个I/O密集的函数被频繁使用相同的参数调用的时候，函数缓存可以节约时间。\n",
    "    在Python 3.2版本以前我们只有写一个自定义的实现。在Python 3.2以后版本，有个lru_cache的装饰器，允许我们将一个函数的返回值快速地缓存或取消缓存。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上下文管理器 (with)\n",
    "    下文管理器允许你在有需要的时候，精确地分配和释放资源。\n",
    "    使用上下文管理器最广泛的案例就是with语句了。\n",
    "    想象下你有两个需要结对执行的相关操作，然后还要在它们中间放置一段代码。\n",
    "    上下文管理器就是专门让你做这种事情的。举个例子："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 标识符长度能有多长 \n",
    "    在Python中，标识符可以是任意长度。此外，我们在命名标识符时还必须遵守以下规则：\n",
    "\n",
    "    只能以下划线或者 A-Z/a-z 中的字母开头\n",
    "    其余部分可以使用 A-Z/a-z/0-9\n",
    "    区分大小写\n",
    "    关键字不能作为标识符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('some_file', 'w') as opened_file:\n",
    "    opened_file.write('Hola!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上面这段代码打开了一个文件，往里面写入了一些数据，然后关闭该文件。\n",
    "# 如果在往文件写数据时发生异常，它也会尝试去关闭文件。上面那段代码与这一段是等价的：\n",
    "file = open('some_file', 'w')\n",
    "try:\n",
    "    file.write('Hola!')\n",
    "finally:\n",
    "    file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当与第一个例子对比时，我们可以看到，通过使用with，许多样板代码(boilerplate code)被消掉了。 这就是with语句的主要优势，它确保我们的文件会被关闭，而不用关注嵌套代码如何退出。\n",
    "上下文管理器的一个常见用例，是资源的加锁和解锁，以及关闭已打开的文件（就像我已经展示给你看的）。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作用域 \n",
    "    全局变量 global\n",
    "    本地变量\n",
    "    非全句变量 nonlocal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" 变量作用域\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "# 基本的全局变量和本地变量\n",
    "varX = \"abc\"\n",
    "varY = \"123\"\n",
    "def func(): \n",
    "    varY = \"456\"\n",
    "    print(\"Func Inner varX: {} , varY: {}\".format(varX,varY)) \n",
    "\n",
    "func()\n",
    "print(\"Func Outer varX: {}, varY: {}\".format(varX,varY))\n",
    "\n",
    "# 本地变量申明为全局变量 global\n",
    "def func_global(): \n",
    "    global varY\n",
    "    varY = \"456\"\n",
    "    print(\"Func Inner varX: {} , varY: {}\".format(varX,varY)) \n",
    "\n",
    "func_global()\n",
    "print(\"Func Outer varX: {}, varY: {}\".format(varX,varY))\n",
    "\n",
    "\n",
    "# 内嵌函数，非全句变量\n",
    "varA = \"Hello\"\n",
    "varB = \"World\"\n",
    "varC = \"nihao\"\n",
    "def func_mather():\n",
    "    varC = \"Hi\"\n",
    "    def func_child():\n",
    "        nonlocal varC\n",
    "        global varA\n",
    "        varA = 'Have a nice day! '\n",
    "        varB = \"hahaha\"\n",
    "        varC = 'ByeBye'\n",
    "        print(\"内嵌子函数： varA:{},varB:{},varC:{}\".format(varA,varB,varC))\n",
    "    func_child()\n",
    "    print(\"母函数： varA:{},varB:{},varC:{}\".format(varA,varB,varC))\n",
    "\n",
    "print(\"调用前：  varA:{},varB:{},varC:{}\".format(varA,varB,varC))\n",
    "func_mather()\n",
    "print(\"全局函数： varA:{},varB:{},varC:{}\".format(varA,varB,varC))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "code_folding": []
   },
   "source": [
    "## 类继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#! /usr/bin/env python\n",
    "# -*- coding: UTF-8 -*-\n",
    "from __future__ import division\n",
    "\n",
    "class People:\n",
    "\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        \n",
    "    def getName(self):\n",
    "        return self.name\n",
    "    \n",
    "    def getAge(self):\n",
    "        return self.age\n",
    "\n",
    "class Student(People):\n",
    "    \n",
    "    def __init__(self,name,age,id):\n",
    "        People.__init__(self, name, age)\n",
    "        self.id = id\n",
    "        \n",
    "    def getId(self):\n",
    "        return self.id\n",
    "\n",
    "class Xdict(dict):\n",
    "    \n",
    "    def __init__(self,new_dict):\n",
    "        dict.__init__(self)\n",
    "        self.new_dict = dict(new_dict)\n",
    "        \n",
    "    def getKeys(self,value):\n",
    "        if value in self.new_dict.values():\n",
    "            key_list = []\n",
    "            for ikey in self.new_dict.keys():\n",
    "                if value in self.new_dict[ikey]:\n",
    "                    key_list.append(ikey)\n",
    "            return key_list\n",
    "        else:\n",
    "            return None\n",
    "\n",
    "        \n",
    "class Vector:\n",
    "    \n",
    "    def __init__(self,x,y,z):\n",
    "        self.vt = (x,y,z)\n",
    "    \n",
    "    def __add__(self,other):\n",
    "        return self.format(map(sum,zip(self.vt,other.vt)))\n",
    "        \n",
    "    def sub(self,si):\n",
    "        return si[0] - si[1]\n",
    "    \n",
    "    def __sub__(self,other):     \n",
    "        return self.format(map(self.sub,zip(self.vt,other.vt)))\n",
    "        \n",
    "    def mul(self,si):\n",
    "        return si[0] * si[1]\n",
    "    \n",
    "    def div(self,si):\n",
    "        return si[0] / si[1]\n",
    "    \n",
    "    def __mul__(self,other):\n",
    "        return self.format(map(self.mul,zip(self.vt,[other]*len(self.vt))))\n",
    "\n",
    "    \n",
    "    def __truediv__(self,other):\n",
    "        return self.format(map(self.div,zip(self.vt,[other]*len(self.vt))))\n",
    "    \n",
    "    def format(self,mps):\n",
    "        for i in range(len(mps)):\n",
    "            mps[i] = float(\"{:.1f}\".format(mps[i]))\n",
    "        return str(tuple(mps))\n",
    "    \n",
    "    def __str__(self): \n",
    "        for i in range(len(self.vt)):\n",
    "            self.vt[i] = float(\"{:.1f}\".format(self.vt[i]))     \n",
    "        return str(tuple(self.vt))\n",
    "        \n",
    "\n",
    "        \n",
    "if __name__ == '__main__':\n",
    "    alist = input().split()\n",
    "    blist = input().split()\n",
    "    n = float(input())\n",
    "    \n",
    "    a = Vector(float(alist[0]),float(alist[1]),float(alist[2]))    \n",
    "    b = Vector(float(blist[0]),float(blist[1]),float(blist[2]))    \n",
    "    \n",
    "    print \"{} {} {} {}\".format(a+b,a-b,a*n,a/n)\n",
    "\n",
    " #==============================================================================\n",
    " #    name = input()\n",
    " #    age = int(input())\n",
    " #    id = input()\n",
    " #    \n",
    " #    S = Student(name,age,id)\n",
    " #    print(\"{} {} {}\".format(S.getName(), S.getAge(),S.getId()))\n",
    " # \n",
    " #    p = People(name,age)\n",
    " #    print(\"{} {}\".format(p.getName(),p.getAge()))\n",
    " #    alist = list(input().split())\n",
    " #    blist = list(input().split())\n",
    " #    target = input()\n",
    " #    d = Xdict(zip(alist,blist))\n",
    " #    print(sorted(d.getKeys(target)))\n",
    " #=============================================================================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    什么是 pep 8？\n",
    "    python 是如何进行内存管理的？\n",
    "    什么是 Python 的命名空间？\n",
    "    Python 中 array和 list 有什么区别？\n",
    "    什么是 pickling 和 unpickling？\n",
    "    Python 中的 Docstrings 是什么？\n",
    "    类继承\n",
    "    9.了解 docstring 么？\n",
    "10.了解类型注解么？\n",
    "Python 垃圾回收机制？\n",
    "76.Python 中的反射了解么?\n",
    "77.metaclass 作用？以及应用场景？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## python的GIL\n",
    "\n",
    "GIL 是python的全局解释器锁，同一进程中假如有多个线程运行，一个线程在运行python程序的时候会霸占python解释器（加了一把锁即GIL），使该进程内的其他线程无法运行，等该线程运行完后其他线程才能运行。如果线程运行过程中遇到耗时操作，则解释器锁解开，使其他线程运行。所以在多线程中，线程的运行仍是有先后顺序的，并不是同时进行。\n",
    "\n",
    "多进程中因为每个进程都能被系统分配资源，相当于每个进程有了一个python解释器，所以多进程可以实现多个进程的同时运行，缺点是进程系统资源开销大"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
